import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login', component: () => import('@/views/UserLogin.vue')
    },
    {
      component: () => import('@/views/LayoutContainer.vue'),
      path: '/layout',
      redirect: '/',
      children: [
        {
          path: '/',
          component: () => import('@/views/PostView.vue')
        },
        {
          path: '/user/comment',
          component: () => import('@/views/user/CommentView.vue')
        },
        {
          path: '/user/info',
          component: () => import('@/views/user/InfoView.vue')
        },
        {
          path: '/user/security',
          component: () => import('@/views/user/SecurityView.vue')
        },
        {
          path: '/user/post',
          component: () => import('@/views/user/PostView.vue')
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.user.username && to.path !== '/login') {
    return '/login'
  }
  return true
})
export default router
